package com.sakiko.audiobook.user.api;

import com.sakiko.audiobook.common.login.SakiLogin;
import com.sakiko.audiobook.common.result.Result;
import com.sakiko.audiobook.common.util.AuthContextHolder;
import com.sakiko.audiobook.user.service.UserListenProcessService;
import com.sakiko.audiobook.vo.user.UserListenProcessVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@Tag(name = "用户声音播放进度管理接口")
@RestController
@RequestMapping("api/user/userListenProcess")
@SuppressWarnings({"all"})
public class UserListenProcessApiController {

    @Autowired
    private UserListenProcessService userListenProcessService;

    /**
     * 获取最近播放记录
     *
     * @return
     */
    @SakiLogin
    @Operation(summary = "获取最近播放记录")
    @GetMapping("/getLatelyTrack")
    public Result getLatelyTrack() {
        Long userId = AuthContextHolder.getUserId();
        Map<String, Object> map = userListenProcessService.getLatelyTrack(userId);
        return Result.ok(map);
    }


    /**
     * 更新声音的播放进度
     *
     * @param userListenProcessVo
     * @return
     */
    @SakiLogin
    @Operation(summary = "更新声音的播放进度")
    @PostMapping("/updateListenProcess")
    public Result updateListenProcess(@RequestBody UserListenProcessVo userListenProcessVo) {
        // 获取用户 Id,这里存的数据类型要对应取的数据类型 UserListenProcess才好，前端 json只传了其中的三个字段，剩下的自己找
        Long userId = AuthContextHolder.getUserId();
        // 调用服务层方法
        userListenProcessService.updateListenProcess(userListenProcessVo, userId);
        return Result.ok();
    }

    /**
     * 获取声音的播放进度
     *
     * @param trackId
     * @return
     */
    @SakiLogin
    @Operation(summary = "获取声音的播放进度")
    @GetMapping("/getTrackBreakSecond/{trackId}")
    public Result getTrackBreakSecond(@PathVariable Long trackId) {
        // 获取用户Id
        Long userId = AuthContextHolder.getUserId();
        // 调用服务层方法
        BigDecimal breakSecond = userListenProcessService.getTrackBreakSecond(trackId, userId);
        // 返回数据
        return Result.ok(breakSecond);
    }

}

